import * as api from './api';
import {
    getCurrentUserPagePermissionsApi,
    getCurrentUserPagePermissionsFail,
    getCurrentUserPagePermissionsSuccess,
} from 'Shared/actions/currentUserContext';
import {PAGE_CODE} from './constants';
import {conventTableCondition} from '../utils';
export const GET_INIT_DATA_BEGIN = 'GET_INIT_DATA_BEGIN';
export const GET_INIT_DATA_SUCCESS = 'GET_INIT_DATA_SUCCESS';
export const GET_INIT_DATA_FAIL = 'GET_INIT_DATA_FAIL';

/*==============================查询action================================= */
//修改查询条件
export const SAVE_QUERY_CONDITION = 'SAVE_QUERY_CONDITION';
export const saveQueryCondition = (name, value) => ({
    type: SAVE_QUERY_CONDITION,
    name,
    value
});

/*==============================初始化API================================= */
export const GET_INIT_BEGIN = 'GET_INIT_BEGIN';
export const GET_INIT_SUCCESS = 'GET_INIT_SUCCESS';
export const GET_INIT_FAIL = 'GET_INIT_FAIL';
export const getInitDataBegin = () => ({
    type: GET_INIT_BEGIN
});
export const getInitDataSuccess = data => ({
    type: GET_INIT_SUCCESS,
    data,
});
export const getInitDataFail = () => ({
    type: GET_INIT_FAIL,
});
export const getInitData = () => dispatch => {
    dispatch(getInitDataBegin());
    const init = api.initQuery().then(res => {
        if(res.ok)
            dispatch(getInitDataSuccess(res.data));
        else
            dispatch(getInitDataFail());
        return res.ok;
    });
    const permissions = getCurrentUserPagePermissionsApi(PAGE_CODE).then(res => {
        if(res.isOk) dispatch(getCurrentUserPagePermissionsSuccess(res.data));
        else dispatch(getCurrentUserPagePermissionsFail(res.statusCode, res.message));
        return res.isOk;
    });
    return Promise.all([init, permissions]).then(values => values[0] && values[1]);
};

/*==============================初始化或清理数据================================= */
//清除数据
export const CLEAR_DATA = 'CLEAR_DATA';
export const clearData = () => ({
    type: CLEAR_DATA,
});
//重置查询条件
export const RESET_QUERY_PANEL = 'RESET_QUERY_PANEL';
export const resetQueryPanel = () => ({
    type: RESET_QUERY_PANEL
});

/*==============================查询列表================================= */
export const GET_LIST_DATA_BEGIN = 'GET_LIST_DATA_BEGIN';
export const GET_LIST_DATA_SUCCESS = 'GET_LIST_DATA_SUCCESS';
export const GET_LIST_DATA_FAIL = 'GET_LIST_DATA_FAIL';

const getListDataBegin = () => ({
    type: GET_LIST_DATA_BEGIN
});

const getListDataSuccess = (condition, data) => ({
    type: GET_LIST_DATA_SUCCESS,
    condition,
    data
});

const getListDataFail = () => ({
    type: GET_LIST_DATA_FAIL
});

//获取查询列表
export const getList = condition => dispatch => {
    dispatch(getListDataBegin());
    const queryCondition = conventTableCondition(condition);
    return api.getList(queryCondition).then(res => {
        if(res.ok)
            dispatch(getListDataSuccess(condition, res.data));
        else
            dispatch(getListDataFail());
    });
};

/*=============================Table数据刷新================================== */
export const tableSearch = page => (dispatch, getState) => {
    const query = getState().getIn(['page', 'appState', 'queryBySort']).toJS();
    if(!query.pageIndex || !page)
        query.pageIndex = 0;
    const condition = Object.assign({}, query, page);
    dispatch(getList(condition));
};

//按钮搜索
export const querySearch = () => (dispatch, getState) => {
    const condition = getState().getIn(['page', 'appState', 'queryCondition']).toJS();
    condition.pageIndex = 0;
    dispatch(getList(condition));
};

//选择查询条件
export const SAVE_QUERY_DEALER = 'SAVE_QUERY_DEALER';
export const saveQueryDealer = data => ({
    type: SAVE_QUERY_DEALER,
    data
});

//导出数据
export const exportData = () => (dispatch, getState) => {
    const condition = getState().getIn(['page', 'appState', 'queryCondition']).toJS();
    const queryCondition = {
        dealerCode: condition.dealerCode,
        dealerName: condition.dealerName,
        shortCode: condition.shortCode,
        shortName: condition.shortName
    };

    return api.exportData(queryCondition).then(res => {
        if(res.ok)
            return res.data;
    });
};

//下载导入模板
export const importTemp = () => () => api.importTemp();
